package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.R;
import com.douwong.student.adapter.AddLocalAppAdapter;
import com.douwong.student.adapter.ApplicationAdapter;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseAppFragment;
import com.douwong.student.contant.Config;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.model.AppInfos;
import com.douwong.student.model.StatusCode;
import com.douwong.student.popuwindow.APKLoadPpopu;
import com.douwong.student.popuwindow.ParentControlPopu;
import com.douwong.student.utils.AppCacheManager;
import com.douwong.student.utils.FileUtils;
import com.douwong.student.utils.LogUtils;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.PreferencesUtils;
import com.douwong.student.utils.ToastUtils;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.MyCommonCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autor yichao
 * @time 2016/11/1517:12
 * @des 应用
 */
@SuppressLint("ValidFragment")
public class ApplicationFragment extends BaseAppFragment {
    private static final String TAG = ApplicationFragment.class.getSimpleName();
    private ArrayList<AppInfos> appInfoList = new ArrayList<AppInfos>();
    private ApplicationAdapter mApplicationAdapter;
    private ArrayList<AppInfos> mLocalList;//本地数据
    private ArrayList<AppInfos> updataList = new ArrayList<>();//更新后的数据
    private APKLoadPpopu mPopu;

    @Override
    protected void initData() {
        super.initData();

        mLocalList = (ArrayList<AppInfos>) AppCacheManager.readObject(getContext(), Config.APPLICATION_RESOURE);
        if (mLocalList != null && !mLocalList.equals("")) {
            appInfoList.clear();
            if (PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH,false)){
                appInfoList.add(getManagerInfo());
                appInfoList.addAll(mLocalList);
                appInfoList.add(getAddAppInfo());

            }else {
                appInfoList.addAll(mLocalList);
            }
            mAppGv.setVisibility(View.VISIBLE);
            mRlFreshContainer.setVisibility(View.INVISIBLE);
            mLlLoading.setVisibility(View.INVISIBLE);
        }
        mApplicationAdapter = new ApplicationAdapter(appInfoList, getActivity());

        mAppGv.setAdapter(mApplicationAdapter);
    }

    @Override
    protected void turnOnPatriarch() {
        super.turnOnPatriarch();
       // appInfoList
        ArrayList<AppInfos> arrayList= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(), Config.APPLICATION_RESOURE);
        if (arrayList!=null&& arrayList.size()!=0){
            //
            appInfoList.clear();
            appInfoList.add(getManagerInfo());
            appInfoList.addAll(arrayList);
            appInfoList.add(getAddAppInfo());
            mApplicationAdapter.setOnLineList(appInfoList);
            mApplicationAdapter.notifyDataSetChanged();

        }

    }

    @Override
    protected void turnOffPatriarch() {
        super.turnOffPatriarch();
        ArrayList<AppInfos> arrayList= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(), Config.APPLICATION_RESOURE);
        if (arrayList!=null&& arrayList.size()!=0){
            //
            appInfoList.clear();
            appInfoList.addAll(arrayList);
            mApplicationAdapter.setOnLineList(appInfoList);
            mApplicationAdapter.notifyDataSetChanged();
        }

    }

    @Override
    protected void loadData() {
        if (!NetUtils.NetwrokIsUseful(getActivity())) {
            ToastUtils.makeText(getActivity(),UIUtils.getString(R.string.please_connection_network));
            return;
        }

        mAppGv.setVisibility(View.INVISIBLE);
        mRlFreshContainer.setVisibility(View.INVISIBLE);
        mLlLoading.setVisibility(View.VISIBLE);
        Map<String, String> map = new HashMap<>();
        map.put(Config.APPTYPE, "747");
        NetUtils.Get(NetConfig.APP_LIST_URL, map, new MyCommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);

                mRlFreshContainer.setVisibility(View.INVISIBLE);
                mLlLoading.setVisibility(View.INVISIBLE);
                mAppGv.setVisibility(View.VISIBLE);
                FileUtils.writeFile(result.getBytes(), Environment.getExternalStorageDirectory().getAbsolutePath(), "app_resoure.txt");
                appInfoList.clear();
                StatusCode<String> sc = (StatusCode<String>) JSON
                        .parseObject(result,
                                new TypeReference<StatusCode<String>>() {
                                });
                if (sc != null && sc.isFlag()) {
                    ArrayList<AppInfos> list = (ArrayList<AppInfos>) JSON.parseArray(sc.getData(), AppInfos.class);//线上的数据
                    if (list != null) {
                        if (mLocalList != null && !mLocalList.equals("")) {
                            updataList.clear();
                            //存在本地数据
                            for (AppInfos appinfo : list) {//线上数据
                                //线上
                                for (AppInfos appLocalInfos:mLocalList) {//本地数据
                                    //线下
                                    if (appLocalInfos.appName.equals(appinfo.appName)) {
                                       /* appLocalInfos.appPageCode = appinfo.appPageCode;
                                        appLocalInfos.appIcoDownloadUrl = appinfo.appIcoDownloadUrl;
                                        appLocalInfos.appName = appinfo.appName;
                                        appLocalInfos.ynControl = appinfo.ynControl;
                                        appLocalInfos.ynShow = appinfo.ynShow;*/
                                        updataList.add(appinfo);
                                    } else {
                                        //判断是否本地，判断线上是否存在
                                        if (appLocalInfos.isLocal&&!updataList.contains(appLocalInfos)){
                                            updataList.add(appLocalInfos);
                                        }
                                    }
                                }

                            }
                            //添加新添加的数据
                            for (AppInfos listData: list) {
                if (!updataList.contains(listData)){
                    updataList.add(listData);

                }
                        }
                            Collections.sort(updataList);
                            //数据存好了
                            if (PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH, false)) {
                                appInfoList.clear();
                                //家长模式
                                appInfoList.add(getManagerInfo());


                                for (int i = 0; i < updataList.size(); i++) {
                                    if (updataList.get(i).ynShow == 1) {
                                        appInfoList.add(updataList.get(i));
                                    }
                                }
                                appInfoList.add(getAddAppInfo());
                            } else {
                                //学生模式
                                appInfoList.clear();
                                for (int i = 0; i < updataList.size(); i++) {
                                    if (updataList.get(i).ynShow == 1) {
                                        appInfoList.add(updataList.get(i));
                                    }
                                }
                            }
                            AppCacheManager.saveObject(getActivity(), updataList, Config.APPLICATION_RESOURE);

                        } else {
                            //本地不存在
                            if (PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH, false)) {
                                //家长模式
                                appInfoList.add(getManagerInfo());
                                for (int i = 0; i < list.size(); i++) {
                                    if (list.get(i).ynShow == 1) {
                                        appInfoList.add(list.get(i));
                                    }
                                }
                                appInfoList.add(getAddAppInfo());

                            } else {
                                //学生模式
                                for (int i = 0; i < list.size(); i++) {
                                    if (list.get(i).ynShow == 1) {
                                        appInfoList.add(list.get(i));
                                    }
                                }
                            }
                            AppCacheManager.saveObject(getActivity(), list, Config.APPLICATION_RESOURE);

                        }
                        mApplicationAdapter.setOnLineList(appInfoList);
                        mApplicationAdapter.notifyDataSetChanged();


                    }

                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                if (appInfoList!=null &&! appInfoList.equals("")){
                    mAppGv.setVisibility(View.VISIBLE);
                    mRlFreshContainer.setVisibility(View.INVISIBLE);
                    mLlLoading.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @Override
    protected void onItemClick(final int position) {
        if (PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH,false)){
            //家长模式
            if (position==0){
                //家长管理
                LogUtils.i(TAG,"家长管理"+position);

                ParentControlPopu popu = new ParentControlPopu(getActivity());
                popu.showAtLocation(getView(), Gravity.CENTER, 0, 0);

            }else if (position==appInfoList.size()-1){

                LogUtils.i(TAG,"添加应用"+position);
                addApplication();

            }else {
                //其他应用
                LogUtils.i(TAG,"其他应用"+position);
                //判读是否上锁
                if (appInfoList.get(position).ynControl==1){
                    //上锁

                    if(appInfoList.get(position).isLocal){
                        //本地
                        setLocalDialog(position);

                    }else {
                        //不是本地
                        setPlatformDialog(position);
                    }


                }else {
                    //没上锁
                    if(appInfoList.get(position).isLocal){
                        //本地
                        setopenLocalDialog(position);

                    }else {
                        //不是本地
                        setOpentPlatformDialog(position);
                    }

                }
            }

        }else {
            //学生模式
            LogUtils.i(TAG,"学生模式"+position);
            if (BaseApplication.isTimeControl){
                openApplication(position);
            }else {
                if (appInfoList.get(position).ynControl == 1) {
                    //上锁
                    final EditText input = new EditText(getActivity());
                    input.setHeight(100);
                    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    final AlertDialog.Builder builde = new AlertDialog.Builder(getActivity());
                    builde.setTitle("输入密码").setIcon(android.R.drawable.ic_dialog_info).setView(input)
                            .setNegativeButton("取消", null);
                    builde.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            String sec = input.getText().toString();
                            String password = PreferencesUtils.getInstance().getString(Config.FAMILY_KEY_PWD, "");
                            if (!password.equals("")){
                                BaseApplication.AppKeyPWD=password;
                            }
                            if (BaseApplication.AppKeyPWD.equals(sec.trim())) {
                                openApplication(position);
                            } else {
                                ToastUtils.makeText(getContext(),"密码错误！");
                            }
                        }
                    });
                    builde.show();
                } else {
                    //没锁
                    openApplication(position);
                }


            }



        }

    }

    /**
     * 设置平台开启密码
     * @param position
     */
    private void setOpentPlatformDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        String[] str3 = new String[]{"打开", "开启密码"};
        builder.setItems(str3, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    //打开应用
                    openApplication(position);
                } else if (which==1){
                    //开启密码
                    appInfoList.get(position).ynControl = 1;
                    ArrayList<AppInfos>  appInfoses= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    for (int i = 0; i < appInfoses.size(); i++) {
                        if (appInfoses.get(i).appName.equals(appInfoList.get(position).appName)){
                            appInfoses.get(i).ynControl=1;

                        }

                    }
                    AppCacheManager.saveObject(getActivity(),appInfoses,Config.APPLICATION_RESOURE);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();
                }else {
                    //移除该应用
                    AppInfos localApp=    appInfoList.get(position);
                    ArrayList<AppInfos>  localAppInfos= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    ArrayList<AppInfos>  clloneLocalAppInfos = (ArrayList<AppInfos>) localAppInfos.clone();

                    for (int i = 0; i < clloneLocalAppInfos.size(); i++) {
                        if (clloneLocalAppInfos.get(i).appName.equals(localApp.appName)){
                            boolean remove = localAppInfos.remove(clloneLocalAppInfos.get(i));

                            LogUtils.i(TAG,"移除本地文件"+remove);
                        }


                    }

                    AppCacheManager.saveObject(getActivity(),localAppInfos,Config.APPLICATION_RESOURE);
                    appInfoList.remove(localApp);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();

                }

            }
        });
        builder.show();

    }

    /**
     * 设置本地开启密码
     * @param position
     */
    private void setopenLocalDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        String[] str3 = new String[]{"打开", "开启密码","移除该应用"};
        builder.setItems(str3, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    //打开应用
                    openApplication(position);
                } else if (which==1){
                    //开启密码
                    appInfoList.get(position).ynControl = 1;
                    ArrayList<AppInfos>  appInfoses= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    for (int i = 0; i < appInfoses.size(); i++) {
                        if (appInfoses.get(i).appName.equals(appInfoList.get(position).appName)){
                            appInfoses.get(i).ynControl=1;

                        }

                    }
                    AppCacheManager.saveObject(getActivity(),appInfoses,Config.APPLICATION_RESOURE);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();
                }else {
                    //移除该应用
                    AppInfos localApp=    appInfoList.get(position);
                    ArrayList<AppInfos>  localAppInfos= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    ArrayList<AppInfos>  clloneLocalAppInfos = (ArrayList<AppInfos>) localAppInfos.clone();

                    for (int i = 0; i < clloneLocalAppInfos.size(); i++) {
                        if (clloneLocalAppInfos.get(i).appName.equals(localApp.appName)){
                            boolean remove = localAppInfos.remove(clloneLocalAppInfos.get(i));

                            LogUtils.i(TAG,"移除本地文件"+remove);
                        }


                    }

                    AppCacheManager.saveObject(getActivity(),localAppInfos,Config.APPLICATION_RESOURE);
                    appInfoList.remove(localApp);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();

                }

            }
        });
        builder.show();


    }

    /**
     * 设置本地应用Dialog
     * @param position
     */
    private void setLocalDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        String[] str3 = new String[]{"打开", "关闭密码","移除该应用"};
        builder.setItems(str3, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    //打开应用
                    openApplication(position);
                } else if (which==1){
                    //关闭密码
                    appInfoList.get(position).ynControl = 0;
                    ArrayList<AppInfos>  appInfoses= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    for (int i = 0; i < appInfoses.size(); i++) {
                        if (appInfoses.get(i).appName.equals(appInfoList.get(position).appName)){
                            appInfoses.get(i).ynControl=0;

                        }

                    }
                    AppCacheManager.saveObject(getActivity(),appInfoses,Config.APPLICATION_RESOURE);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();
                }else {
                    //移除该应用
                    AppInfos localApp=    appInfoList.get(position);
                    ArrayList<AppInfos>  localAppInfos= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    ArrayList<AppInfos>  clloneLocalAppInfos = (ArrayList<AppInfos>) localAppInfos.clone();

                    for (int i = 0; i < clloneLocalAppInfos.size(); i++) {
                        if (clloneLocalAppInfos.get(i).appName.equals(localApp.appName)){
                            boolean remove = localAppInfos.remove(clloneLocalAppInfos.get(i));

                            LogUtils.i(TAG,"移除本地文件"+remove);
                        }

                    }
                    AppCacheManager.saveObject(getActivity(),localAppInfos,Config.APPLICATION_RESOURE);
                    appInfoList.remove(localApp);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();

                }

            }
        });
        builder.show();


    }

    /**
     * 设置平台应用
     * @param position
     */
    private void setPlatformDialog(final int position) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        String[] str3 = new String[]{"打开", "关闭密码"};
        builder.setItems(str3, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    //打开应用
                    openApplication(position);
                } else {
                    //关闭密码
                    appInfoList.get(position).ynControl = 0;
                    ArrayList<AppInfos>  appInfoses= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
                    for (int i = 0; i < appInfoses.size(); i++) {
                        if (appInfoses.get(i).appName.equals(appInfoList.get(position).appName)){
                            appInfoses.get(i).ynControl=0;

                        }

                    }
                    AppCacheManager.saveObject(getActivity(),appInfoses,Config.APPLICATION_RESOURE);
                    mApplicationAdapter.setOnLineList(appInfoList);
                    mApplicationAdapter.notifyDataSetChanged();
                }

            }
        });
        builder.show();
    }

    /**
     * 打开应用
     * @param position
     */
    private void openApplication(int position) {
       AppInfos appInfoL= appInfoList.get(position);
       if ( appInfoL.isLocal){
           //本地
           try {
               Intent book1 = getActivity().getPackageManager().getLaunchIntentForPackage(appInfoL.packageName);
               book1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               startActivity(book1);
           } catch (Exception e) {
               Toast.makeText(getActivity(), "应用未安装！", Toast.LENGTH_LONG).show();
               e.printStackTrace();
           }

       }else {
           //平台
           try {
               Intent book1 = getActivity().getPackageManager().getLaunchIntentForPackage(appInfoL.appPackageName);
               book1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               startActivity(book1);
           } catch (Exception e) {

               e.printStackTrace();
               //下载
               if (Environment.getExternalStorageState().equals(
                       Environment.MEDIA_MOUNTED)) {
                   FileUtils.APK_DIR = Environment.getExternalStorageDirectory()
                           .getAbsolutePath() + "/douwong/apks";
                   File apkDir=new File(FileUtils.APK_DIR);
                   if(!apkDir.exists()){
                       apkDir.mkdirs();
                   }
                   mPopu = new APKLoadPpopu(getActivity(), appInfoL,getView());
                   mPopu.downFile();
               }else{
                 ToastUtils.makeText(getActivity(),"本地存储设备异常，无法下载保存！！");
               }

           }


       }
    }

    /**
     * 添加应用
     */
    private void addApplication() {
        final Dialog   appDialog = new Dialog(getActivity());
        appDialog.setTitle("本地应用");
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.app_system, null);
        appDialog.setContentView(view);
        ListView app_list = (ListView) view.findViewById(R.id.app_list);
    final      ArrayList<AppInfos> list = new ArrayList<AppInfos>();
        PackageManager pm = getActivity().getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (int i = 0; i < packages.size(); i++) {
            PackageInfo packageInfo = packages.get(i);
            boolean isExist = false;
  final           ArrayList<AppInfos> localDate = (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(), Config.APPLICATION_RESOURE);

            if (localDate != null && localDate.size() > 0) {

                for (int j = 0; j < localDate.size(); j++) {
                    AppInfos appInfos = localDate.get(j);
                    if (packageInfo.packageName.equals(appInfos.packageName)
                            || packageInfo.packageName.equals(appInfos.appPackageName)) {
                        isExist = true;
                    }
                }
                if (!isExist) {
                    if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                        AppInfos newApp = new AppInfos();
                        newApp.appName = packageInfo.applicationInfo.loadLabel(pm).toString();
                        newApp.packageName = packageInfo.packageName;
                        newApp.versionName = packageInfo.versionName;
                        newApp.versionCode = packageInfo.versionCode;
                        newApp.isInner = false;
                        newApp.isLocal = true;
                        newApp.ynControl = 1;
                        newApp.ynShow=1;
                        if (!getActivity().getPackageName().equals(newApp.packageName)) {
                            list.add(newApp);
                        }
                    }


                }

                //显示数据
                AddLocalAppAdapter addLocalAppAdapter=new AddLocalAppAdapter(getActivity(),list);
                app_list.setAdapter(addLocalAppAdapter);
                app_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //点击
                        AppInfos appInfos = list.get(position);
                        addLocalData(appInfos);

                        appDialog.dismiss();


                    }
                });

                appDialog.show();

            }
        }
    }

    /**
     * 添加本地数据
     * @param appInfos
     */
    private void addLocalData(AppInfos appInfos) {

      ArrayList<AppInfos> mlocalData= (ArrayList<AppInfos>) AppCacheManager.readObject(getActivity(),Config.APPLICATION_RESOURE);
        if (mlocalData!=null&& mlocalData.size()>0){
            if (PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH,false)){
                //家长模式
                appInfoList.clear();
                appInfoList.add(getManagerInfo());
                mlocalData.add(appInfos);
                Collections.sort(mlocalData);
                appInfoList.addAll(mlocalData);
                appInfoList.add(getAddAppInfo());
            }else {
                //学生模式
                mlocalData.add(appInfos);
                appInfoList.addAll(mlocalData);
            }
            AppCacheManager.saveObject(getActivity(),mlocalData,Config.APPLICATION_RESOURE);
            mApplicationAdapter.setOnLineList(appInfoList);
            mApplicationAdapter.notifyDataSetChanged();
        }


    }

    /**
     * 家长管理信息
     *
     * @return
     */
    private AppInfos getManagerInfo() {
        AppInfos appInfos = new AppInfos();
        appInfos.appName = "家长管理";
        appInfos.packageName = "manager";
        appInfos.isLocal = false;
        return appInfos;
    }

    /**
     * 添加信息
     *
     * @return
     */
    private AppInfos getAddAppInfo() {
        AppInfos appInfos = new AppInfos();
        appInfos.appName = "添加应用";
        appInfos.packageName = "add";
        appInfos.isLocal = false;
        return appInfos;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPopu!=null && mPopu.isShowing()){
            mPopu.dismiss();
        }
    }
}
